<?php

/**
 * This is the model class for table "{{article}}".
 *
 * The followings are the available columns in table '{{article}}':
 * @property integer $id
 * @property string $class_id
 * @property string $title
 * @property string $des
 * @property integer $user_id
 * @property string $content
 * @property string $kw
 * @property string $create_time
 * @property integer $update_time
 * @property integer $status
 */
class Article extends CActiveRecord
{
    //设置文章发表状态常量
	const STATUS_DRAFT = 1;
	const STATUS_PUBLISHED = 2;
	const STATUS_ARCHIVED = 3;
        
	/**
	 * Returns the static model of the specified AR class.
	 * @return Article the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{article}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('class_id, title, content, kw, status', 'required'),
			array('status', 'in', 'range'=>array(1,2,3)),
			array('content', 'length', 'min'=>100),
			array('title', 'length', 'max'=>80),
			array('des', 'length', 'max'=>128),
			array('kw', 'length', 'max'=>30),
			array('content', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('title, des,content, kw', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		    'author' => array(self::BELONGS_TO, 'User', 'user_id'),
			'classify' => array(self::BELONGS_TO, 'Classify', 'class_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'class_id' => '分类',
			'title' => '标题',
			'des' => '描述',
			'user_id' => '作者',
			'content' => '内容',
			'kw' => '关键词',
			'create_time' => '发布时间',
			'update_time' => '更新时间',
			'status' => 'Status',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

                $criteria->compare('status',$this->class_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'status, update_time DESC',
			),
		));
	}
	
	
	protected function beforeSave()
	{
	    if(parent::beforeSave())
		{
		    //如果是一个更新动作
		    if($this->isNewRecord)
			{
			    $this->create_time=$this->update_time=time();
				$this->user_id=Yii::app()->user->id;
			}
			else
			    $this->update_time=time();
			return true;
		}
		else
		    return true;
	}
	
	/**
	 * 替换Lookup::items()方法
	 */
	public static function getArticleStatus()
	{
	    return array(
	        self::STATUS_DRAFT => 'Draft',
	        self::STATUS_PUBLISHED => 'Published',
	        self::STATUS_ARCHIVED => 'Archived',
	    );
	}
        
    /**
	 * 它用来显示article的详情(对于含get的方法，可直接使用$model->url进行访问)
	 * @return string url
	 */
	public function getUrl()
	{
		return Yii::app()->createUrl('article/view', array(
			'id'=>$this->id,
			'user_id'=>$this->user_id,
			'title'=>$this->title,
			'des'=>$this->des,
			'kw'=>$this->kw,
			'status'=>$this->status,
		));
	}
    /**
	 * 返回字符串类型的 URL。它用来显示classify的详情(对于含get的方法，可直接使用$model->url2进行访问)
	 */
    public function getUrl2()
	{
		return Yii::app()->createUrl('classify/view', array(
			'id'=>$this->class_id,
			'title'=>$this->title,
		));
	}
	/**
	 * 返回字符串类型的 作者名。
	 */
	public function getAuthor($id)
	{    
	    return User::model()->FindByPk($id);
	}
	
	/**
	 * 参数类型整数：要返回文章的总体数
	 * 返回值是数组类型：即最近增加的评论
	 */
	public function findArticles($limit)
	{
		$criteria = new CDbCriteria;
		//$criteria->limit = $limit ? $limit : 10;
		$criteria->limit = $limit;
		$criteria->order = 'create_time desc,id desc';
		
		$results=self::model()->findAll($criteria);
        $result=array();
		foreach ($results as $key=>$value)
		{
			$result[$value->id]=$value->title; 
		}
		return $result;
	}
	
	public function getJudge($value)
	{
	    switch ($value) {   
            case 1:$value="草稿";
			    break;    
            case 2:$value="已发布";
			    break; 
			case 3:$value="已归档" ;
			    break; 
			default:break;
            } ;
		return $value;
	}
}